﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using UFSSWebApp.UFSSservicereference;

namespace UFSSWebApp
{
    public partial class search : System.Web.UI.Page
    {
        UFSSservicereference.UserCreds uc = new UFSSservicereference.UserCreds();
        UFSSservicereference.UFSSserviceSoapClient ussc = new UFSSservicereference.UFSSserviceSoapClient();
        protected void Page_Load(object sender, EventArgs e)
        {
            lblError.Visible = false;
            uc = (UFSSservicereference.UserCreds)Session["usercreds"];
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                string searchtype = ddlSearch.SelectedItem.Value;
                string query = txtSearch.Text;
                switch (searchtype)
                {
                    case "Files":
                        {
                            UFSSservicereference.UFSSFile[] files = ussc.searchFiles(uc, query);

                            //create dataset
                            DataSet ds = new DataSet();
                            DataTable dt = new DataTable("files");
                            DataRow dr;

                            //create columns
                            DataColumn namecolumn, sizecolumn, ownercolumn, datecolumn, md5column, downloadscolumn;
                            namecolumn = new DataColumn("Filename");
                            sizecolumn = new DataColumn("Size");
                            ownercolumn = new DataColumn("Owner");
                            datecolumn = new DataColumn("Date added");
                            downloadscolumn = new DataColumn("Times downloaded");
                            md5column = new DataColumn("md5");



                            //insert columns
                            dt.Columns.Add(namecolumn);
                            dt.Columns.Add(sizecolumn);
                            dt.Columns.Add(ownercolumn);
                            dt.Columns.Add(datecolumn);
                            dt.Columns.Add(downloadscolumn);
                            dt.Columns.Add(md5column);


                            //create headers

                            //fill table
                            foreach (UFSSservicereference.UFSSFile file in files)
                            {
                                dr = dt.NewRow();
                                dr["Filename"] = file.filename.ToString();
                                dr["Size"] = file.filesize.ToString() + " bytes";
                                dr["Owner"] = ussc.getUserName(file.userid);
                                dr["Date added"] = file.dateadded.ToString();
                                dr["Times downloaded"] = file.downloads.ToString();
                                dr["md5"] = file.hash.ToString();

                                dt.Rows.Add(dr);
                            }
                            ds.Tables.Add(dt);

                            //populate gridview
                            DataView dv = new DataView(ds.Tables["files"]);
                            dv = ds.Tables[0].DefaultView;


                            gvSearch.DataSource = ds.Tables["files"];
                            gvSearch.AutoGenerateColumns = true;
                            gvSearch.DataBind();
                            if (files[0] != null)
                                for (int i = 0; i < files.Length; i++)
                                {

                                    gvSearch.Rows[i].Cells[0].Text = "<a href=\"userfile.aspx?id=" + files[i].ownerid + "\">" + files[i].filename + "</a>";
                                    gvSearch.Rows[i].Cells[2].Text = "<a href=\"userpage.aspx?id=" + gvSearch.Rows[i].Cells[2].Text + "\">" + gvSearch.Rows[i].Cells[2].Text + "</a>";

                                }
                            gvSearch.Visible = true;



                            break;
                        }
                    case "Users":
                        {
                            ArrayOfString arrayofusernames = ussc.searchUsers(uc, txtSearch.Text);
                            if (arrayofusernames == null)
                                break;
                            string[] usernames = arrayofusernames.ToArray();

                            //create dataset
                            DataSet ds = new DataSet();
                            DataTable dt = new DataTable("users");
                            DataRow dr;

                            //create columns
                            DataColumn usernamecolumn, friendscolumn, filescolumn, subscriptionscolumn;

                            usernamecolumn = new DataColumn("Username");



                            //insert columns
                            dt.Columns.Add(usernamecolumn);


                            //create headers

                            //fill table
                            if (usernames != null)
                                foreach (string username in usernames)
                                {

                                    dr = dt.NewRow();
                                    dr["Username"] = username;
                                    dt.Rows.Add(dr);
                                }
                            ds.Tables.Add(dt);

                            //populate gridview
                            DataView dv = new DataView(ds.Tables["users"]);
                            dv = ds.Tables[0].DefaultView;
                            gvSearch.DataSource = ds.Tables["users"];
                            gvSearch.DataBind();


                            for (int i = 0; i < usernames.Length; i++)
                            {
                                gvSearch.Rows[i].Cells[0].Text = "<a href=\"userprofile.aspx?id=" + usernames[i].ToString() + "\">" + usernames[i].ToString() + "</a>";
                            }

                            gvSearch.Visible = true;
                            break;
                        }
                    case "Subscriptions":
                        {
                            UFSSservicereference.UFSSSubscription[] subs = ussc.searchSubs(uc, query);

                            //create dataset
                            DataSet ds = new DataSet();
                            DataTable dt = new DataTable("subs");
                            DataRow dr;

                            //create columns
                            DataColumn namecolumn, memberscolumn, ownercolumn, datecolumn, filescolumn, typecolumn;
                            namecolumn = new DataColumn("Name");
                            memberscolumn = new DataColumn("Members");
                            ownercolumn = new DataColumn("Owner");
                            datecolumn = new DataColumn("Last update");
                            filescolumn = new DataColumn("Files");
                            typecolumn = new DataColumn("Type");


                            //insert columns
                            dt.Columns.Add(namecolumn);
                            dt.Columns.Add(memberscolumn);
                            dt.Columns.Add(ownercolumn);
                            dt.Columns.Add(datecolumn);
                            dt.Columns.Add(filescolumn);
                            dt.Columns.Add(typecolumn);

                            //create headers

                            //fill table
                            foreach (UFSSservicereference.UFSSSubscription sub in subs)
                            {
                                dr = dt.NewRow();
                                dr["Name"] = sub.subName;
                                dr["Members"] = sub.members;
                                dr["Owner"] = sub.ownername;
                                dr["Last update"] = sub.lastup.ToString();
                                dr["Files"] = sub.files.ToString();
                                dr["Type"] = sub.type;
                                dt.Rows.Add(dr);
                            }
                            ds.Tables.Add(dt);

                            //populate gridview
                            DataView dv = new DataView(ds.Tables["subs"]);
                            dv = ds.Tables[0].DefaultView;
                            gvSearch.DataSource = ds.Tables["subs"];
                            gvSearch.DataBind();
                            for (int i = 0; i < subs.Length; i++)
                            {
                                gvSearch.Rows[i].Cells[0].Text = "<a href=\"usersubs.aspx?id=" + subs[i].subid + "\">" + subs[i].subName + "</a>";
                                gvSearch.Rows[i].Cells[2].Text = "<a href=\"userpage.aspx?id=" + subs[i].ownername + "\">" + subs[i].ownername + "</a>";
                            }


                            gvSearch.Visible = true;
                            break;
                        }
                    default:
                        {

                            break;
                        }
                }
            }
            catch
            {
                lblError.Visible = true;
                lblError.Text = "There was an error completing your request... please try again later.";
            }
        }

        protected void gvSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
;
        }

        protected void gvSearch_RowCommand(object sender, GridViewCommandEventArgs e)
        {

            
            
        }


    }
}